<?php
class Controller extends Object {
	public $library = null;
	public $path = null;
	public $pagination = null;
	public $pageURL = null;
	public $page = 0;
	public $REQUEST_URI = null;
	public $datacontrol = null;
	public $security = null;
	public $template = null;
	public $smarty = null;
	
	function __construct()
	{
		if( isset($_REQUEST['pageURL'])){
			$this->pageURL = $_REQUEST['pageURL'];
		}
		if(isset($_REQUEST['page'])) {
			$this->page = $_REQUEST['page'];
		}
		$this->REQUEST_URI = '';
		global $datacontrol, $security, $template, $smarty;
		$this->datacontrol = $datacontrol;
		$this->security = $security;
		$this->template = $template;
		$this->smarty = $smarty;

	}
	
	static function instance($controller_name)
	{
		//die($controller_name);
		$controller_file_name = '';
		$file_not_exist = array();
		$array_controller = explode('.', $controller_name);
		$controller_file_name = str_replace('.', '', $controller_name);
		//die($controller_name.']');
		//die(PATH_CONTROLLER."Controller_".$controller_name.EXT);
		if(file_exists(PATH_CONTROLLER."Controller_".$controller_file_name.EXT)){
			require_once(PATH_CONTROLLER."Controller_".$controller_file_name.EXT);
			$script = '$controller =  new Controller_'.$controller_file_name."();";
			$script .= '$controller->path = "'.str_replace("\\", "\\\\", $controller_file_name.DIRECTORY_SEPARATOR).'";';
			$script .= '$controller->library = library::instance(\''.$controller_name.'\')';
			$script .= '$controller->pagination = Helper::instance("Pagination");';
			//die(var_dump($script));
			eval($script);
			//$controller->objects = array();
			return $controller;
		}
		$file_not_exist[] = PATH_CONTROLLER."Controller_".$controller_file_name.EXT;
		
		$folder_name = '';
		
		if( count($array_controller) > 1) {
			$folder_name = strtolower($array_controller[0]).DIRECTORY_SEPARATOR;
		} else {
			$folder_name = strtolower($controller_file_name).DIRECTORY_SEPARATOR;
		}
		//die(var_dump($folder_name));
		//die(WEBROOT.SITE_RELATIVE.'modules'.DIRECTORY_SEPARATOR.$folder_name."Controller_".$controller_name.EXT);
		
		if(file_exists(WEBROOT.SITE_RELATIVE.'modules'.DIRECTORY_SEPARATOR.$folder_name."Controller_".$controller_file_name.EXT)){
			require_once(WEBROOT.SITE_RELATIVE.'modules'.DIRECTORY_SEPARATOR.$folder_name."Controller_".$controller_file_name.EXT);
			$script = '$controller = new Controller_'.$controller_file_name."();";
			$script .= '$controller->path = "'.str_replace("\\", "\\\\", $folder_name).'";';
			$script .= '$controller->library = library::instance("'.$controller_name.'");';
			$script .= '$controller->pagination = Helper::instance("Pagination");';
			//die(var_dump($script));
			//die($script);
			eval($script);
			//var_dump($controller);
			//die();
			//$controller->objects = array();
			return $controller;
		}

		$file_not_exist[] = WEBROOT.SITE_RELATIVE.'modules'.DIRECTORY_SEPARATOR.$folder_name."Controller_".$controller_file_name.EXT;
		
		echo("The following controller file(s) cannot be found on server:<br />");
		foreach( $file_not_exist as $key=>$val){
			echo "<b>".$val."</b> not exists!<br />";
		}
		die();
	}
}
?>